<template>
  <div class="container">
    <div class="app-container">
      <div class="search">
        <el-form class="search-form">
          <el-form-item label="设备编号：" label-width="120px">
            <el-input
              v-model="pageParams.innerCode"
              placeholder="请输入"
              style="width: 200px"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              class="search-btn"
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="searchVm"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="list">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column width="80px" label="序号">
            <template v-slot="{ row }"> {{ row.index }} </template>
          </el-table-column>
          <el-table-column prop="innerCode" label="设备编号"> </el-table-column>
          <el-table-column prop="type.name" label="设备型号"> </el-table-column>
          <el-table-column prop="vmCount" label="详细地址">
            <template v-slot="{ row }"
              ><span>{{
                row.node.addr.split("-").splice(3).join("")
              }}</span></template
            >
          </el-table-column>
          <el-table-column prop="contact" label="运营状态">
            <template v-slot="{ row }">
              {{
                row.vmStatus === 0
                  ? "未投放"
                  : row.vmStatus === 1
                  ? "运营"
                  : row.vmStatus === 3
                  ? "撤机"
                  : ""
              }}
            </template>
          </el-table-column>
          <el-table-column prop="account" label="设备状态" width="280px">
            <template>
              <div class="cells">
                <div class="label offline">离线</div>
                <div class="label goods">货道</div>
                <div class="label jiku">传动轴</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <el-button type="text" @click="getVminfo(row)"
                >查看详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="space-between" style="margin-top: 20px">
          <span class="pageCount"
            >共{{ pageParams.totalCount }}条记录 第{{ pageParams.pageIndex }}/{{
              pageParams.totalPage
            }}页</span
          >
          <el-pagination
            layout="slot"
            :current-page.sync="pageParams.pageIndex"
            :page-size="pageParams.pagesize"
            :total="pageParams.totalPage"
          >
            <el-button
              type="primary"
              :disabled="pageParams.pageIndex === 1 ? true : false"
              class="pagi-btn"
              :class="{ disabled: pageParams.pageIndex === 1 }"
              @click="goPrev"
              >上一页</el-button
            >
            <el-button
              type="primary"
              :disabled="
                pageParams.pageIndex === pageParams.totalPage ? true : false
              "
              :class="{
                disabled: pageParams.pageIndex === pageParams.totalPage,
              }"
              class="pagi-btn"
              @click="goNext"
              >下一页</el-button
            >
          </el-pagination>
        </el-row>
      </div>
      <!-- 弹窗 -->
      <!-- 查看详情 -->
      <el-dialog
        title="设备详情"
        :visible="showInfoDialog"
        width="630px"
        @close="showInfoDialog = false"
      >
        <div class="info-title">
          <div class="title-item">
            销售量：<span class="num">{{ OrderCount }} 个</span>
          </div>
          <div class="title-item">
            销售额：<span class="num">{{ OrderAmount }} 万元</span>
          </div>
          <div class="title-item">
            补货次数：<span class="num">{{ SupplyCount }} 次</span>
          </div>
          <div class="title-item">
            维修次数：<span class="num">{{ RepairCount }} 次</span>
          </div>
        </div>
        <div class="table-title">商品销量（月）</div>
        <div class="table" v-if="SkuCollect.length">
          <div
            class="table-item"
            v-for="(item, index) in SkuCollect"
            :key="index"
          >
            <p>{{ item.skuName }}</p>
            <span>: {{ item.count }}</span>
          </div>
        </div>
        <div v-else class="warn">当前设备未销售商品</div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  searchVmApi,
  getOrderCountApi,
  getOrderAmountApi,
  getSupplyCountApi,
  getRepairCountApi,
  getSkuCollectApi,
} from "@/api/vm";
import { dateRangeApiSecF, dateRangeApi } from "@/utils/time";
export default {
  name: "Region",
  components: {},
  data() {
    return {
      tableData: [],
      showAddDialog: false,
      showSetDialog: false,
      showInfoDialog: false,
      OrderCount: "",
      OrderAmount: "",
      SupplyCount: "",
      RepairCount: "",
      SkuCollect: "",
      pageParams: {
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        totalCount: 0,
        innerCode: "",
      },
    };
  },
  created() {
    this.searchVm();
  },
  methods: {
    // 搜索
    async searchVm() {
      const res = await searchVmApi({
        ...this.pageParams,
      });
      this.tableData = [...res.currentPageRecords];
      this.pageParams.pageIndex = +res.pageIndex;
      this.pageParams.pageSize = +res.pageSize;
      this.pageParams.totalPage = +res.totalPage;
      this.pageParams.totalCount = +res.totalCount;

      // 序号
      let pageNow = this.pageParams.pageIndex;
      this.tableData.forEach((item, index) => {
        this.$set(
          item,
          "index",
          pageNow > 1 ? (pageNow - 1) * 10 + index + 1 : index + 1
        );
      });
    },

    async getVminfo(data) {
      
      this.OrderCount = await getOrderCountApi({
        innerCode: data.innerCode,
        start: dateRangeApiSecF.start,
        end: dateRangeApiSecF.end,
      });
      const res = await getOrderAmountApi({
        innerCode: data.innerCode,
        start: dateRangeApiSecF.start,
        end: dateRangeApiSecF.end,
      });
      this.OrderAmount = (res / 1000000).toFixed(2);
      this.SupplyCount = await getSupplyCountApi(
        {
          innerCode: data.innerCode,
          start: dateRangeApi.start,
          end: dateRangeApi.end,
        },
        {
          vmType: data.vmType,
          nodeId: data.nodeId,
          createUserId: data.createUserId,
        }
      );
      this.RepairCount = await getRepairCountApi(
        {
          innerCode: data.innerCode,
          start: dateRangeApi.start,
          end: dateRangeApi.end,
        },
        {
          vmType: data.vmType,
          nodeId: data.nodeId,
          createUserId: data.createUserId,
        }
      );
      this.SkuCollect = await getSkuCollectApi(
        {
          innerCode: data.innerCode,
          start: dateRangeApi.start,
          end: dateRangeApi.end,
        },
        {
          vmType: data.vmType,
          nodeId: data.nodeId,
          createUserId: data.createUserId,
        }
      );
      this.showInfoDialog = true;
    },
    // 上一页
    goPrev() {
      this.pageParams.pageIndex--;
      this.searchVm();
    },
    // 下一页
    goNext() {
      this.pageParams.pageIndex++;
      this.searchVm();
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  background-color: #f8fafd;
  padding: 20px;
  .search {
    background-color: #fff;
    display: flex;
    height: 64px;
    align-items: center;
    padding-top: 23px;
    margin-bottom: 20px;
    .search-form {
      display: flex;
      align-items: center;
      .search-btn {
        background-color: #5f84ff;
        border: none;
        margin-left: 15px;
        font-size: 16px;
        height: 36px;
        width: 80px;
        text-align: center;
        padding: 0;
      }
    }
  }
  .list {
    // height: 673px;
    padding: 20px;
    background-color: #fff;

    .btns {
      margin-bottom: 15px;
    }
    .add-btn {
      width: 80px;
      text-align: center;
      padding: 12px 0px;
      background-color: #ff8035;
      border: none;
      font-size: 14px;
    }
    .setting-btn {
      border: none;
      background-color: #fbf4f0;
      color: #716762;
      width: 80px;
      text-align: center;
      padding: 12px 0px;
    }
    .pageCount {
      color: #dbdfe5;
      margin-left: 10px;
    }
  }
  .pagi-btn {
    background-color: #d5ddf8;
    color: #606266;
    width: 70px;
    height: 32px;
    text-align: center;
  }
  .disabled {
    background-color: #eceff8;
    color: #d2d7dd;
  }
  .del {
    color: #ff5a5a;
  }
}
.cells {
  display: flex;
}
.label {
  height: 23px;
  padding: 3px 12px;
  border-radius: 14px;
  color: #fff;
  font-size: 14px;
  line-height: 18px;
  margin-right: 9px;
}
.offline {
  background-color: #ffbd26;
}
.goods {
  background-color: #48d697;
}
.jiku {
  background-color: #48d697;
}
.info-title {
  width: 579px;
  height: 54px;
  background-color: #f4f6fb;
  display: flex;
  align-items: center;
  justify-content: space-evenly;
  .num {
    color: #698cff;
  }
}
.table-title {
  margin-top: 20px;
  margin-bottom: 10px;
}
.table {
  display: flex;
  flex-wrap: wrap;
  .table-item {
    width: 144px;
    height: 41px;
    border: 1px solid #d8dde3;
    display: flex;
    padding: 0 10px;
    p {
      width: 80px;
      height: 30px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    span{
      line-height: 41px;
    }
  }
}
  .warn{
    width: 100%;
    text-align: center;
  }
</style>
